import {
  login,
  logout,
  getInfo
} from "@/api/user";
import {
  getToken,
  setToken,
  removeToken,
} from "@/utils/auth";
import {
  resetRouter
} from "@/router";

const avatar =
  "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_USERID: (state, userId) => {
    state.userId = userId;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_ROLEID: (state, roleId) => {
    state.roleId = roleId;
  },
};

const actions = {
  // 获取权限列表
  async getRoles({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      // 请求权限列表，这里模拟请求已获取到的权限列表,跟路由表中的path一一对应，(需要树状数据!!!)
      const roles = [{
        path: "/book",
        children: [{
          path: "book_list"
        }, {
          path: "book_info"
        }],
      },
      ];
      console.log("roles", roles)
      resolve(roles);
    });
  },

  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo;
    return new Promise((resolve, reject) => {
      login({
        userName: username.trim(),
        password: password
      })
        .then((response) => {
          const {
            data
          } = response;
          commit("SET_TOKEN", data.token);
          setToken(data.token);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const {
            data
          } = response;
          if (!data) {
            return reject("Verification failed, please Login again.");
          }
          const {
            userName,
            roleId,
            id
          } = data;
          commit("SET_NAME", userName);
          commit("SET_ROLEID", roleId);
          commit("SET_USERID", id);
          commit("SET_AVATAR", avatar);
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout()
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          commit("RESET_STATE");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise((resolve) => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
